import { VantComponent } from '../common/component';
import wepy from 'wepy';
///底部版权模块
VantComponent({
  props: {
    btmargin:{type:String,value:'34rpx'},
    watchNode:{
      type:String,
      value:'#xkdmaster'
    },
    positionValue:{
      type:String,
      value:'absolute'
    }
  },
  data:{
    isclose:true,
    visibility:"hidden",//visible
    oemname:"",
    oemLogo:"",
    position:"relative",//absolute/relative/fixed
  },
  mounted:function(){
    let that=this; 
    that.checkNumber=0;
    that.checkInitStatus();
  },
  methods: {
    //检查初始化状态
    checkInitStatus:function()
    {
      let that=this;
      let handler=setTimeout(()=>{
        clearTimeout(handler);
        if(wepy.$instance.globalData.oemInfo){
          that.setInitData();///初始化数据
        }else
        {
          if(that.checkNumber<20)
          {
            that.checkNumber++;
            that.checkInitStatus();
          }
        }
      },1000);
    },
    setInitData:function(){
      let isclose=  wepy.$instance.globalData.closeCopyright;
      if(!isclose){
        let oemLogo=  wepy.$instance.globalData.oemInfo.Logo;
        this.setData({oemLogo:oemLogo });
        let oemname=  `${wepy.$instance.globalData.oemInfo.Name}`;
        this.setData({oemname:oemname });
        this.setData({isclose:isclose });
        this.resetPosition(1000);
      }
    },
    resetPosition:function(timeOut){
      //return;
      let that=this;
      let tnumber=setTimeout(() => {
        
        if(!that.selectNode)
        {

          const query = wx.createSelectorQuery();
          that.selectNode=query.select(that.data.watchNode);
          const sysRes = wx.getSystemInfoSync();
          that.windowHeight=sysRes.windowHeight;
        }
       
        that.selectNode.fields({size:true},(res) => {
            if(!res) {
                return;
            }
            if((res.height+10)>=that.windowHeight)
            {
                 if(that.data.position==that.data.positionValue)
                 {
                  that.setData({position:"relative"});
                 }
            }
            else{
                if(that.data.position=="relative")
                 {
                  that.setData({position:that.data.positionValue});
                 }
            }

            that.setData({visibility:"visible"});
              clearTimeout(tnumber);
            }).exec();
        }, timeOut);
        
    },
    showCopyright:function (){
      
      ////重置版权位置
      if(this.data.isclose){
        return;///不做处理
      }
      
      this.resetPosition(800);
      
    },
    onClick:function onClick() {
      this.$emit('redirectToPage');
    }
  }
});